home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / BORLAND TURBO ASSEMBLER / WAP.PAK / WAP.ASM < prev    next >
Assembly Source File  |  1996-01-10  |  10KB  |  315 lines

  1. ;           Copyright (c) 1993 by Borland International, Inc.
  2. ;
  3. ;                   * Borland Turbo Assembler 4.0 *
  4. ;
  5. ;              * Windows Application Example in Assembly *
  6. ;
  7. ;     This example (WAP.ASM) will put up a window and beep when the
  8. ;     right mouse button is pressed.  When the left mouse button is
  9. ;     pressed, it will put up a message box.
  10. ;
  11.  
  12. locals
  13. jumps
  14. .model large, WINDOWS PASCAL
  15. include windows.inc
  16.  
  17. extrn   BEGINPAINT:PROC
  18. extrn   CREATEWINDOW:PROC
  19. extrn   DEFWINDOWPROC:PROC
  20. extrn   DISPATCHMESSAGE:PROC
  21. extrn   ENDPAINT:PROC
  22. extrn   GETMESSAGE:PROC
  23. extrn   GETSTOCKOBJECT:PROC
  24. extrn   INITAPP:PROC
  25. extrn   INITTASK:PROC
  26. extrn   INVALIDATERECT:PROC
  27. extrn   LOADCURSOR:PROC
  28. extrn   MESSAGEBEEP:PROC
  29. extrn   MESSAGEBOX:PROC
  30. extrn   POSTQUITMESSAGE:PROC
  31. extrn   REGISTERCLASS:PROC
  32. extrn   SHOWWINDOW:PROC
  33. extrn   TEXTOUT:PROC
  34. extrn   TRANSLATEMESSAGE:PROC
  35. extrn   UPDATEWINDOW:PROC
  36. extrn   WAITEVENT:PROC
  37. .data
  38.  
  39.             db 16 dup (0)         ; Filler for Windows Task manager.
  40.                                   ;  This *MUST* be declared, otherwise
  41.                                   ;  Windows will clobber part of your data
  42.                                   ;  segment.  For additional information on
  43.                                   ;  Windows Task managment and what
  44.                                   ;  happens when Windows apps start up, see
  45.                                   ;  "Windows Internals" - Matt Pietrek,
  46.                                   ;  1993 Addison Wesley
  47. psp         dw ?
  48. pszCmdline  dw ?
  49. hPrev       dw ?
  50. hInstance   dw ?
  51. cmdShow     dw ?
  52.  
  53. newhwnd     dw 0
  54. lppaint     PAINTSTRUCT <0>
  55. msg         MSGSTRUCT   <0>
  56. wc          WNDCLASS    <0>
  57. mbx_count   dw 0
  58.  
  59. szTitleName     db 'Windows Assembly Program',0
  60. szClassName     db 'ASMCLASS',0
  61. szMsg           db 'Hello there folks',0
  62. szCapt          db 'Left Mouse',0
  63. szPaint         db 'There are '
  64. s_num           db '0 MessageBoxes waiting.',0
  65. MSG_L EQU ($-offset szPaint)-1
  66. .code
  67. .286
  68. ;-----------------------------------------------------------------------------
  69. start:
  70.  
  71.                 mov     ax, @data
  72.                 mov     ds, ax           ; set up data segment
  73.  
  74. ;Windows initialization.  Sets up registers and stack.
  75.  
  76.                 ;INITTASK returns:
  77.                 ;  Failure:
  78.                 ;    AX = zero if it failed
  79.                 ;  Success:
  80.                 ;    AX = 1
  81.                 ;    CX = stack limit
  82.                 ;    DX = cmdShow parameter to CreateWindow
  83.                 ;    ES:BX = -> DOS format command line (ES = PSP address)
  84.                 ;    SI = hPrevinstance
  85.                 ;    DI = hinstance
  86.  
  87.                 call    INITTASK
  88.                 or      ax,ax
  89.                 jnz     @@OK
  90.                 jmp     @@Fail
  91. @@OK:           mov     [psp],es
  92.                 mov     word ptr [pszCmdline],bx
  93.                 mov     [hPrev],si
  94.                 mov     [hInstance],di
  95.                 mov     [cmdShow],dx
  96.  
  97. ;Initialize the Windows App
  98.  
  99.                 xor     ax,ax
  100.                 push    ax
  101.                 call    WAITEVENT
  102.                 push    [hInstance]
  103.                 call    INITAPP
  104.                 or      ax,ax
  105.                 jnz     @@InitOK
  106.  
  107. @@Fail:
  108.                 mov     ax, 4CFFh
  109.                 int     21h          ; terminate program
  110.  
  111.  
  112. @@InitOK:
  113. ;-----------------------------------------------------------------------------
  114. ; This is generally where WinMain is called.  We won't use a WinMain, since
  115. ;  this app is 100% assembly.
  116.  
  117.                  cmp    [hPrev], 0
  118.                  jne    already_running
  119.  
  120.                ; initialize the WndClass structure
  121.                  mov    [wc.clsStyle], CS_HREDRAW + CS_VREDRAW
  122.                  mov    word ptr [wc.clsLpfnWndProc], offset WndProc
  123.                  mov    word ptr [wc.clsLpfnWndProc+2], seg WndProc
  124.                  mov    [wc.clsCbClsExtra], 0
  125.                  mov    [wc.clsCbWndExtra], 0
  126.                  mov    ax, [hInstance]
  127.                  mov    [wc.clsHInstance], ax
  128.                  mov    [wc.clsHIcon], 0
  129.  
  130.                  push   0
  131.                  push   IDC_ARROW
  132.                  call   LOADCURSOR
  133.                  mov    [wc.clsHCursor], ax
  134.  
  135.                  push   WHITE_BRUSH
  136.                  call   GETSTOCKOBJECT
  137.                  mov    [wc.clsHbrBackground], ax
  138.  
  139.                  mov    word ptr [wc.clsLpszMenuName], 0
  140.                  mov    word ptr [wc.clsLpszMenuName+2], 0
  141.  
  142.                  mov    word ptr [wc.clsLpszClassName], offset szClassName
  143.                  mov    word ptr [wc.clsLpszClassName+2], ds
  144.  
  145.  
  146.                  push   ds
  147.                  push   offset wc
  148.                  call   REGISTERCLASS
  149.  
  150. already_running:
  151.  
  152.                  push   ds
  153.                  push   offset szClassName     ; Class name
  154.                  push   ds
  155.                  push   offset szTitleName     ; Title string
  156.                  push   WS_OVERLAPPEDWINDOW+WS_VISIBLE    ; high word of Style
  157.                  push   0                      ; low word of Style
  158.                  push   CW_USEDEFAULT          ; x
  159.                  push   CW_USEDEFAULT          ; y
  160.                  push   CW_USEDEFAULT          ; width
  161.                  push   CW_USEDEFAULT          ; height
  162.                  push   0                      ; parent hwnd
  163.                  push   0                      ; menu
  164.                  push   [hInstance]            ; hInstance
  165.                  push   0                      ; lpParam
  166.                  push   0                      ; lpParam
  167.  
  168.                  call   CREATEWINDOW
  169.  
  170.                  mov    [newhwnd], ax
  171.  
  172.                  push   [newhwnd]
  173.                  push   [cmdShow]
  174.                  call   SHOWWINDOW
  175.  
  176.                  push   [newhwnd]
  177.                  call   UPDATEWINDOW
  178.  
  179. msg_loop:
  180.                  push   ds
  181.                  push   offset msg
  182.                  push   0
  183.                  push   0
  184.                  push   0
  185.                  call   GETMESSAGE
  186.  
  187.                  cmp    ax, 0
  188.                  je     end_loop
  189.  
  190.                  push   ds
  191.                  push   offset msg
  192.                  call   TRANSLATEMESSAGE
  193.  
  194.                  push   ds
  195.                  push   offset msg
  196.                  call   DISPATCHMESSAGE
  197.  
  198.                  jmp    msg_loop
  199.  
  200. end_loop:
  201.                  mov    ax, [msg.msWPARAM]
  202.                  mov    ah, 4Ch
  203.                  int    21h
  204.  
  205. ;-----------------------------------------------------------------------------
  206. WndProc     proc hwnd:WORD, wmsg:WORD, wparam:WORD, lparam:DWORD
  207.  
  208.                 cmp     [wmsg], WM_DESTROY
  209.                 je      wmdestroy
  210.                 cmp     [wmsg], WM_LBUTTONDOWN
  211.                 je      wmlbuttondown
  212.                 cmp     [wmsg], WM_CREATE
  213.                 je      wmcreate
  214.                 cmp     [wmsg], WM_RBUTTONDOWN
  215.                 je      wmrbuttondown
  216.                 cmp     [wmsg], WM_PAINT
  217.                 je      wmpaint
  218.  
  219.                 jmp     defwndproc
  220.  
  221. wmpaint:
  222.                 push    [hwnd]
  223.                 push    ds
  224.                 push    offset lppaint
  225.                 call    BEGINPAINT
  226.  
  227.                 push    ax              ; the DC
  228.  
  229.  
  230.                 mov     bx, [mbx_count]
  231.                 add     bl, '0'
  232.                 mov     [s_num], bl
  233.  
  234.                 push    5               ; x
  235.                 push    5               ; y
  236.  
  237.                 push    ds
  238.                 push    offset szPaint  ; string
  239.  
  240.                 push    MSG_L           ; length of string
  241.  
  242.                 call    TEXTOUT
  243.  
  244.                 push    [hwnd]
  245.                 push    ds
  246.                 push    offset lppaint
  247.                 call    ENDPAINT
  248.  
  249.                 mov     ax, 0
  250.                 jmp     finish
  251.  
  252. wmcreate:
  253.                 mov     ax, 0
  254.                 jmp     finish
  255.  
  256. defwndproc:
  257.                 push    hwnd
  258.                 push    wmsg
  259.                 push    wparam
  260.                 push    lparam
  261.                 call    DEFWINDOWPROC
  262.                 jmp     finish
  263.  
  264. wmdestroy:
  265.                 push    0
  266.                 call    POSTQUITMESSAGE
  267.                 mov     ax, 0
  268.                 jmp     finish
  269.  
  270. wmlbuttondown:
  271.                 cmp     [mbx_count], 5
  272.                 jae     finish
  273.  
  274.                 inc     [mbx_count]
  275.  
  276.                 push    [hwnd]
  277.                 push    0
  278.                 push    0
  279.                 push    0
  280.                 call    INVALIDATERECT    ; repaint window
  281.  
  282.                 push    0
  283.                 push    ds
  284.                 push    offset szMsg
  285.                 push    ds
  286.                 push    offset szCapt
  287.                 push    0
  288.                 call    MESSAGEBOX        ; put up msgbox and wait
  289.                 mov     ax, 0
  290.                 dec     [mbx_count]
  291.  
  292.                 push    [hwnd]
  293.                 push    0
  294.                 push    0
  295.                 push    0
  296.                 call    INVALIDATERECT   ; repaint window again
  297.  
  298.  
  299.                 jmp     finish
  300.  
  301. wmrbuttondown:
  302.                 push    0
  303.                 call    MESSAGEBEEP
  304.                 jmp     finish
  305.  
  306. finish:
  307.                 mov     dx, 0
  308.                 ret
  309. WndProc         endp
  310. ;-----------------------------------------------------------------------------
  311. public WndProc
  312. ends
  313. end start
  314.  
  315.